﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using System.Threading;
using System.Net;
using System.IO;
using System.Web.Security;
namespace camcad7Home.CultureHelper
{
    public class CultureController : Controller
    {
        protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
        {
            string cultureName = "vi-VN";
            HttpCookie cultureCookie;
            // Attempt to read the culture cookie from Request
            if (Request.Cookies["_culture"] == null)
            {
                cultureCookie = new HttpCookie("_culture");

                cultureName = CultureHelper.GetDefaultCulture();
                cultureCookie["lang"] = cultureName;
                Response.Cookies.Add(cultureCookie);
            }
            else
            {
                cultureCookie = Request.Cookies["_culture"];

                if (cultureCookie != null)
                {
                    cultureName = cultureCookie["lang"];
                    cultureName = CultureHelper.GetValidCulture(cultureName); // This is safe
                }
                else
                {
                    cultureName = CultureHelper.GetDefaultCulture();
                    cultureCookie["lang"] = cultureName;
                    Response.Cookies.Add(cultureCookie);
                }
            }

            // Modify current thread's cultures   
            System.Globalization.CultureInfo cultureme = new System.Globalization.CultureInfo(cultureName);
            cultureme.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
            cultureme.DateTimeFormat.DateSeparator = "/";
            Thread.CurrentThread.CurrentCulture = cultureme;
            Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

            return base.BeginExecuteCore(callback, state);
        }

    }
}
